///////////////////////////////////////////////////////////////////////
//  CS 3715 (Winter 2012), Final Project                      		 //
//  Program File Name: PizzaServlet.java                           	 //
//       Student Name: Jonathan Hart-Wall, Kenny Hardiman            //
//         Login Name: jcwhw1, kch004                                //
//              MUN #: 200933455, 200930253    						 //
///////////////////////////////////////////////////////////////////////

package ca.FMP;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import ca.FMP.DBAccess;

/*
 * PizzaServlet Class, used to receive HTTPrequests from the Order page, and sends XML to final page 
 */
public class PizzaServlet extends HttpServlet {

	/*
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse) doGet method that recieves
	 * HTTPRequests, and sends XML
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		/*
		 * Sets response type to xml. Sets PizzaPlaceName to parameter of
		 * pizzaplace, and sets order to parameter of order. Sets pizzas to
		 * array of order split by blank space
		 */
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		String PizzaPlaceName = request.getParameter("pizzaplace");
		String order = request.getParameter("order");
		String[] pizzas = order.split(" ");

		/*
		 * XML response, that returns Order, PPName, Address, PhoneNumber,
		 * OTime, CTime of Pizza Place, and NumPizza, Tax, OrderPrice, OriPrice
		 * of current order
		 */
		out.println("<?xml version='1.0'?>");
		out.println("<Order> \n");
		for (int i = 0; i < DBAccess.database.db.size(); i++) {
			if (DBAccess.database.db.get(i).getName().equals(PizzaPlaceName)) {
				if (PizzaPlaceName.equals("AtlasPizza")) {
					out.println("<PPName>Atlas Pizza</PPName> \n");
				} else if (PizzaPlaceName.equals("BigCheesePizza")) {
					out.println("<PPName>Big Cheese Pizza</PPName> \n");
				} else if (PizzaPlaceName.equals("BostonPizza")) {
					out.println("<PPName>Boston Pizza</PPName> \n");
				} else if (PizzaPlaceName.equals("ColossalPizza")) {
					out.println("<PPName>Colossal Pizza</PPName> \n");
				} else if (PizzaPlaceName.equals("DominosPizza")) {
					out.println("<PPName>Dominos Pizza</PPName> \n");
				} else if (PizzaPlaceName.equals("Mr.JimPizza")) {
					out.println("<PPName>Mr.Jim's Pizza</PPName> \n");
				} else if (PizzaPlaceName.equals("NoNamePizza")) {
					out.println("<PPName>No Name Pizza</PPName> \n");
				} else if (PizzaPlaceName.equals("OldTownPizzeria")) {
					out.println("<PPName>Old Town Pizzeria</PPName> \n");
				} else if (PizzaPlaceName.equals("PetersPizza")) {
					out.println("<PPName>Peter's Pizza</PPName> \n");
				} else if (PizzaPlaceName.equals("PizzaDelight")) {
					out.println("<PPName>Pizza Delight</PPName> \n");
				} else if (PizzaPlaceName.equals("PizzaEmpire")) {
					out.println("<PPName>Pizza Empire</PPName> \n");
				} else if (PizzaPlaceName.equals("PizzaHut")) {
					out.println("<PPName>Pizza Hut</PPName> \n");
				} else if (PizzaPlaceName.equals("PizzaPros")) {
					out.println("<PPName>Pizza Pros</PPName> \n");
				} else if (PizzaPlaceName.equals("PizzaSupreme")) {
					out.println("<PPName>Pizza Supreme</PPName> \n");
				} else if (PizzaPlaceName.equals("TopsailRoadPizzeria")) {
					out.println("<PPName>Topsail Road Pizzeria</PPName> \n");
				} else {
					out.println("<PPName>Venice Pizza</PPName> \n");
				}
				out.println("<Address>"
						+ (DBAccess.database.db.get(i).getAddress())
						+ "</Address> \n");
				out.println("<PhoneNumber>"
						+ (DBAccess.database.db.get(i).getPhoneNumber())
						+ "</PhoneNumber> \n");
				out.println("<OTime>"
						+ (DBAccess.database.db.get(i).getOpeningTime())
						+ "</OTime>");
				out.println("<CTime>"
						+ (DBAccess.database.db.get(i).getClosingTime())
						+ "</CTime> \n");

				double OverallPrice = 0.0;
				int PizzaNum = 0;
				for (int j = 0; j < pizzas.length; j = j + 2) {
					double pizzaPrice = 0.0;
					if (pizzas[j].equals("s")) {
						pizzaPrice = pizzaPrice
								+ (DBAccess.database.db.get(i).getSmallPrice());
					} else if (pizzas[j].equals("med")) {
						pizzaPrice = pizzaPrice
								+ (DBAccess.database.db.get(i).getMedPrice());
					} else if (pizzas[j].equals("l")) {
						pizzaPrice = pizzaPrice
								+ (DBAccess.database.db.get(i).getLargePrice());
					} else if (pizzas[j].equals("xl")) {
						pizzaPrice = pizzaPrice
								+ (DBAccess.database.db.get(i).getXLargePrice());
					} else {
						pizzaPrice = pizzaPrice
								+ (DBAccess.database.db.get(i).getPartyPrice());
					}
					int NumTop = Integer.parseInt(pizzas[j + 1]);
					double TopPrice = (NumTop * (DBAccess.database.db.get(i)
							.getToppingPrice()));

					pizzaPrice = pizzaPrice + TopPrice;

					OverallPrice = OverallPrice + pizzaPrice;
					PizzaNum = PizzaNum + 1;
				}

				double oP = OverallPrice;
				int ix0 = (int) (oP * 100.0);
				oP = ((double) ix0) / 100.0;

				double OTax = (OverallPrice * 0.13);
				int ix1 = (int) (OTax * 100.0);
				OTax = ((double) ix1) / 100.0;

				OverallPrice = OverallPrice + OTax;
				int ix = (int) (OverallPrice * 100.0);
				OverallPrice = ((double) ix) / 100.0;
				OverallPrice = OverallPrice + 4.00;

				out.println("<NumPizza>" + PizzaNum + "</NumPizza>");
				out.println("<Tax>" + OTax + "</Tax> \n");
				out.println("<OrderPrice>" + OverallPrice + "</OrderPrice> \n");
				out.println("<OriPrice>" + oP + "</OriPrice>");
			}
		}
		out.println("</Order>");
	}
}